package com.wan37.logic.fighting.hand;

import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.protobuf.constant.*;
import com.wan37.logic.domain.Result;
import com.wan37.logic.domain.vo.*;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.service.cache.MonsterServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.skill.impl.SkillManager;
import com.wan37.logic.utils.DelayDequeThreadPollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : luoyong
 * @date : 2020-06-28 12:13
 **/
@Component
public class MonsterAttackPlayer implements Fighting {
    private DelayDequeThreadPollUtil delayDequeThreadPollUtil;
    private SceneServiceCache sceneServiceCache;
    private MonsterServiceCache monsterServiceCache;
    private SkillManager skillManager;
    @Autowired
    private RoleServiceCache roleServiceCache;
    @Autowired
    private UserServiceCache userServiceCache;

    @Override
    public Result start(FightingVO fightingVO) {
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.skillManager = SpringContextHolder.getBean("skillManager");
        this.monsterServiceCache = SpringContextHolder.getBean("monsterServiceCache");

        Map<String, SkillVO> skillVOMap = monsterServiceCache.getMonsterServiceMap().get(fightingVO.getAllRoleId()).getSkillVOMap();
        SkillVO skillVO = skillVOMap.get(fightingVO.getSkillId());
        Map<String, AllRoleVO> allRoleVOMap = sceneServiceCache.getSceneServiceMap().get(fightingVO.getSceneId()).getAllRoleVOMap();
        AllRoleVO allRoleVO = allRoleVOMap.get(fightingVO.getAllRoleId());
        List<String> allRoleIdList = fightingVO.getAllRoleIdList();
        List<AllRoleVO> allRoleVOList = new ArrayList<>();
        for (String allRoleId : allRoleIdList) {
            allRoleVOList.add(roleServiceCache.getRoleServiceMap().get(allRoleId).getRoleVO());
        }
        for (String userId : sceneServiceCache.getSceneServiceMap().get(fightingVO.getSceneId()).getUserIdSet()) {
            for (String roleId : userServiceCache.getUserServiceMap().get(userId).getRoleIdList()) {
                if (!allRoleIdList.contains(roleId)) {
                    RoleVO roleVO = roleServiceCache.getRoleServiceMap().get(roleId).getRoleVO();
                    if (roleVO.getStatus() != AllRoleLabel.STATUS_DIE) {
                        allRoleVOList.add(roleVO);
                    }
                }
            }
        }
        if (monsterServiceCache.getMonsterServiceMap().get(fightingVO.getAllRoleId()).getSkillVOMap().isEmpty()) {
            return new Result(CommonFighting.ATTACK_ERROR, "boss没有任何技能，无法攻击!!!");

        }
        if (sceneServiceCache.getSceneServiceMap().get(fightingVO.getSceneId())
                .getAllRoleVOMap().get(fightingVO.getAllRoleId()).getStatus() == AllRoleLabel.STATUS_DIE) {
            return new Result(CommonFighting.ATTACK_ERROR, "boss已经死亡，无法攻击!!!");
        }
        if (skillVO == null) {

            return new Result(CommonFighting.ATTACK_ERROR, "boss未学习该技能！！");

        }
        if (allRoleVO == null || allRoleIdList == null) {

            return new Result(CommonFighting.ATTACK_ERROR, "目标不存在！！！");
        }
        //计算消耗
        return cost(skillVO, allRoleVO, allRoleVOList);
    }

    @Override
    public Result cost(SkillVO skillVO, AllRoleVO allRoleVO, List<AllRoleVO> allRoleVOList) {
        //检验角色剩余HP是否能够释放该技能。
        if (allRoleVO.getNumberMap().get(CommonBase.HP) - skillVO.getCostHP() < 0) {
            return new Result(CommonFighting.ATTACK_ERROR, "没蓝了！！！");
        }
        if (allRoleVOList.isEmpty()) {
            return new Result(CommonFighting.ATTACK_ERROR, "角色已被击杀！！！");
        }

        Map<String, Map<String, Long>> allRoleVONumberMap = null;

        if (skillVO.getLabel().startsWith(SkillLabel.AOE)) {
            //技能释放处理双方数值
            allRoleVONumberMap = skillManager.handler(skillVO, allRoleVO, allRoleVOList);
        } else if (skillVO.getLabel().startsWith(SkillLabel.ONE)) {
            //技能释放处理双方数值
            allRoleVONumberMap = skillManager.handler(skillVO, allRoleVO, allRoleVOList.get(0));
        }

        Result result = finish(skillVO, allRoleVO, allRoleVOList);
        //后续结果处理
        return new SuccessFightingResult(result.getCode(), result.getContent(), allRoleVONumberMap);

    }

    @Override
    public Result finish(SkillVO skillVO, AllRoleVO allRoleVO, List<AllRoleVO> allRoleVOList) {
        this.delayDequeThreadPollUtil = SpringContextHolder.getBean(("delayDequeThreadPollUtil"));
        for (AllRoleVO allRoleVO2 : allRoleVOList) {
            if (allRoleVO2.getNumberMap().get(CommonBase.MP) <= 0) {
                //角色状态设置为0，代表角色已死亡
                allRoleVO2.setStatus(AllRoleLabel.STATUS_DIE);
            }
        }
        return new Result(CommonFighting.ATTACK_SUCCESS, "");
    }
}
